package com.xunqi.mall.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xunqi.mall.enums.ResponseEnum;
import com.xunqi.mall.form.ShippingAddForm;
import com.xunqi.mall.mapper.ShippingMapper;
import com.xunqi.mall.pojo.Shipping;
import com.xunqi.mall.service.ShippingService;
import com.xunqi.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-24 15:13
 **/
@Service("shippingService")
public class ShippingServiceImpl implements ShippingService {

    @Resource
    private ShippingMapper shippingMapper;

    /**
     * 添加收获地址
     * @param uid
     * @param form
     * @return
     */
    @Override
    public ResponseVo addShipping(Integer uid, ShippingAddForm form) {

        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(form,shipping);

        shipping.setUserId(uid);
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());

        int result = shippingMapper.insertSelective(shipping);

        //新增失败
        if (result <= 0) {
            return ResponseVo.error(ResponseEnum.ERROR);
        }

        Map<String,Integer> map = new HashMap<>();
        map.put("shippingId",shipping.getId());

        return ResponseVo.success(map);
    }


    /**
     * 删除用户收获地址
     * @param uid
     * @param shippingId
     * @return
     */
    @Override
    public ResponseVo deleteShipping(Integer uid, Integer shippingId) {

        //判断收获id是否为空
        if (StringUtils.isEmpty(shippingId)) {
            return ResponseVo.error(ResponseEnum.SHIPPING_NOTEXIST);
        }

        int result = shippingMapper.deleteByPrimaryKey(shippingId);

        //删除失败
        if (result <= 0) {
            return ResponseVo.error(ResponseEnum.DELETE_SHIPPING_ERROR);
        }

        return ResponseVo.success();
    }

    /**
     * 更新收获地址
     * @param uid
     * @param form
     * @return
     */
    @Override
    public ResponseVo updateShipping(Integer shippingId,Integer uid, ShippingAddForm form) {

        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(form,shipping);

        shipping.setUserId(uid);
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());
        shipping.setId(shippingId);

        //执行更新操作
        int result = shippingMapper.updateByPrimaryKeySelective(shipping);

        //更新失败
        if (result <= 0) {
            return ResponseVo.error(ResponseEnum.UPDATE_SHIPPING_ERROR);
        }

        return ResponseVo.success();
    }


    /**
     * 分页显示收获地址信息
     * @param uid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseVo<PageInfo> shippingList(Integer uid, Integer pageNum, Integer pageSize) {

        //设置分页条件
        PageHelper.startPage(pageNum,pageSize);

        List<Shipping> shippingList = shippingMapper.selectAll(uid);

        PageInfo page = new PageInfo(shippingList);

        return ResponseVo.success(page);
    }
}
